package controller

import (
	"log"
	"net/http"

	"gin-xorm/app"
	"gin-xorm/database"
	"gin-xorm/models"

	"github.com/gin-gonic/gin"
)

type UserController struct {
	UserDao database.UserDao
}

// @Summary List users
// @Description list user
// @Produce  json
// @Param phone query string true "phone"
// @Success 200 {string} string	"ok"
// @Failure 400 {object} app.Response
// @Failure 404 {object} app.Response
// @Router /api/user [get]
func (c UserController) List(ctx *gin.Context) {
	phone := ctx.Query("phone")
	user := *c.UserDao.QueryUser(phone)
	if user == nil {
		ctx.JSON(http.StatusOK, app.Response{
			Code: 400,
			Msg:  "没有记录",
			Data: user,
		})
		return
	}
	ctx.JSON(http.StatusOK, app.Response{
		Code: 0,
		Msg:  "",
		Data: user,
	})

}

func Login(ctx *gin.Context) {
	// 获取参数
	name := ctx.Query("name")
	password := ctx.Query("password")
	// 数据验证tt
	if len(name) < 1 {
		ctx.JSON(http.StatusBadRequest, app.Response{
			Code: 400,
			Msg:  "用户名不能为空",
		})
		return
	}
	if len(password) < 1 {
		ctx.JSON(http.StatusBadRequest, app.Response{
			Code: 400,
			Msg:  "密码不能r为空",
		})
		return
	}
	// 判断数据是否存在
	if name == "dayd" {
		token, err := database.ReleaseToken(models.User{Id: 5})
		cLog.Info(token)
		if err != nil {
			log.Print(err)
			return
		}
		ctx.JSON(200, app.Response{
			Code: 0,
			Data: token,
		})

	}
	// 发放token
	// 返回结果

}
